package com.xinke.module.system.service.sms;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.xinke.module.system.model.vo.sms.*;
import com.xinke.module.system.model.entity.sms.SmsLogDO;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.util.object.BeanUtils;

import com.xinke.module.system.mapper.sms.SmsLogMapper;

import static com.xinke.component.common.exception.util.ServiceExceptionUtil.exception;
import static com.xinke.module.system.enums.ErrorCodeConstants.*;

/**
 * 短信日志 Service 实现类
 *
 * @author yzx
 */
@Service
@Validated
public class SmsLogServiceImpl implements SmsLogService {

    @Resource
    private SmsLogMapper smsLogMapper;

    @Override
    public Long createSmsLog(SmsLogSaveReqVO createReqVO) {
        // 插入
        SmsLogDO smsLog = BeanUtils.toBean(createReqVO, SmsLogDO.class);
        smsLogMapper.insert(smsLog);
        // 返回
        return smsLog.getLogId();
    }

    @Override
    public void updateSmsLog(SmsLogSaveReqVO updateReqVO) {
        // 校验存在
        validateSmsLogExists(updateReqVO.getLogId());
        // 更新
        SmsLogDO updateObj = BeanUtils.toBean(updateReqVO, SmsLogDO.class);
        smsLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteSmsLog(Long logId) {
        // 校验存在
        validateSmsLogExists(logId);
        // 删除
        smsLogMapper.deleteById(logId);
    }

    private void validateSmsLogExists(Long logId) {
        if (smsLogMapper.selectById(logId) == null) {
            throw exception(null);
        }
    }

    @Override
    public SmsLogDO getSmsLog(Long logId) {
        return smsLogMapper.selectById(logId);
    }

    @Override
    public PageResult<SmsLogDO> getSmsLogPage(SmsLogPageReqVO pageReqVO) {
        return smsLogMapper.selectPage(pageReqVO);
    }

}